﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;
using Chinesys.Domain.DataMapper;
using Chinesys.Domain.InputValidations;
using Chinesys.Domain.BizRules;

namespace Chinesys.Domain
{
    public static class OrderDetailDomain
    {
        public static List<OrderDetailEntity> GetAll()
        {
            List<OrderDetailEntity> list;

            using (ChinesysEntities db = new ChinesysEntities())
            {
                list = (from o in db.OrderDetails
                        select OrderDetailDataMapper.OrderDetailFromRowToEntity(o)).ToList<OrderDetailEntity>(); 
            }

            return list;
        }


        public static List<OrderDetailEntity> GetAllByOrderId(int id)
        {
            List<OrderDetailEntity> list;

            using (ChinesysEntities db = new ChinesysEntities())
            {
                list = (from o in db.OrderDetails
                        where o.OrderId == id
                        select OrderDetailDataMapper.OrderDetailFromRowToEntity(o)).ToList<OrderDetailEntity>();
            }

            return list;
        }

        public static List<OrderDetailEntity> GetAllByOrderIdAndProductId(int orderId, int prodId)
        {
            List<OrderDetailEntity> list;

            using (ChinesysEntities db = new ChinesysEntities())
            {
                list = (from o in db.OrderDetails
                        where o.OrderId == orderId && o.ProductId == prodId
                        select OrderDetailDataMapper.OrderDetailFromRowToEntity(o)).ToList<OrderDetailEntity>();
            }

            return list;
        }

        public static OrderDetailEntity Save(OrderDetailEntity entity)
        {
            OrderDetailInputValidation validate = new OrderDetailInputValidation(entity);
            OrderDetailBizRules.OnSave(entity);

            if (entity.CurrentState == RowState.New)
                entity = Inser(entity);

            if (entity.CurrentState == RowState.Modified)
                entity = Update(entity);

            if (entity.CurrentState == RowState.Deleted)
                Delete(entity);

            return entity;
        }

        private static void Delete(OrderDetailEntity entity)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                OrderDetail row = db.OrderDetails.SingleOrDefault(x => x.Id == entity.Id);

                if (row == null)
                    throw new BizRulesException("No Se encuentra el numero de deatalle de la orden. No se puede eliminar el detalle de la orden.");

                db.OrderDetails.DeleteObject(row);
            }
        }

        private static OrderDetailEntity Inser(OrderDetailEntity entity)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                OrderDetail orderDetailRow = OrderDetailDataMapper.OrderDetailFromEntityToRow(entity);
                db.AddToOrderDetails(orderDetailRow);
                entity.Id = orderDetailRow.Id;
            }

            return entity;
        }

        private static OrderDetailEntity Update(OrderDetailEntity entity)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                OrderDetail row = db.OrderDetails.SingleOrDefault(x => x.Id == entity.Id);

                if (row == null)
                    throw new BizRulesException("No Se encuentra el numero de deatalle de la orden. No se puede realizar la modificacion.");

                row.OrderId = entity.OrderId;
                row.ProductId = entity.ProductId;
                row.Quantity = entity.Quantity;
                row.UnitPrice = entity.UnitPrice;

                db.SaveChanges();
            }

            return entity;
        }
    }
}
